package com.example.demo.treeDemo;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;

import java.util.List;

/**
 * @author pengyifeng
 * @version 5.0
 * @ClassName TestTre.java
 * @Description hutool 树形构造方法
 * @createTime 2021年04月29日 10:29:00
 */
public class TestTree {
    public static void main(String[] args) {
        List<TreeNode<String>> nodeList= CollUtil.newArrayList();
        nodeList.add(new TreeNode<>("1","0","系统管理",5));
        nodeList.add(new TreeNode<>("11", "1", "用户管理", 222222));
        nodeList.add(new TreeNode<>("111", "11", "用户添加", 0));
        nodeList.add(new TreeNode<>("2", "0", "店铺管理", 1));
        nodeList.add(new TreeNode<>("21", "2", "商品管理", 44));
        nodeList.add(new TreeNode<>("221", "2", "商品管理2", 2));
        List<Tree<String>> build = TreeUtil.build(nodeList, "0");
        System.out.println(build);

        //自定义字段
        TreeNodeConfig treeNodeConfig=new TreeNodeConfig();
        treeNodeConfig.setWeightKey("order");
        treeNodeConfig.setIdKey("rid");
        treeNodeConfig.setDeep(3);

        List<Tree<String>> build1 = TreeUtil.build(nodeList, "0", treeNodeConfig, (treeNode, tree) -> {
            tree.setId(treeNode.getId());
            tree.setParentId(treeNode.getParentId());
            tree.setWeight(treeNode.getWeight());
            tree.setName(treeNode.getName());
            // 扩展属性 ...
            tree.putExtra("extraField", 666);
            tree.putExtra("other", new Object());
        });

        System.out.println(build1);
    }
}
